<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Role extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    public function index() {
        $html = $this->init("權限設定", "jquery", "table");
        $this->load->model('role_model');
        $html["role"] = $this->role_model->get_all();
        $this->load->view('role', $html);
    }

    public function add() {
        $html = $this->init("權限設定", "jquery", "form");
        $this->load->model('department_model');
        $html["department"] = $this->department_model->get_all();
        $this->load->view('role_add', $html);
    }

    public function add_submit() {
        $this->load->model('role_model');
        $data['role_name'] = $this->input->post('role_name');
        $data['department_id'] = $this->input->post('department_id');
        $data['description'] = $this->input->post('description');
		
		$data['sercet_level'] 	= 1;
		$data['is_public'] 		= "Y";
		$data['important'] 		= 1;
		$data['is_see'] 		= "Y";		

		
        //$data['purview'] = $this->input->post('purview');
        //$data['sequence'] = $this->input->post('sequence');
        $return_result = $this->role_model->add($data);
        if ($return_result > 0) {
            redirect("/role");
        } else {
            echo "插入失敗";
        }
    }

    public function purview($id = null) {
        $html = $this->init("權限", "jquery,purview", "form");
        $this->load->model('system_module_model');
        $html["system_module"] = $this->system_module_model->get_all();
        $this->load->model('purview_model');
        $html["purview"] = $this->purview_model->get_purview();
        //print_r($html["purview"]);
        $this->load->model('role_model');
        $html["role"] = $this->role_model->get_info($id);
        
        $this->load->view('purview', $html);
    }

    public function purview_submit() {
        $this->load->model('role_model');
        $data["id"] = $this->input->post('role_id');
        $purview = $this->input->post('purview');
		
		//print_r($_POST);
		
        $p = "";
        foreach ($purview as $key => $val) {
            if ($p == "") {
                $p.="," . $val . ",";
            } else {
                $p.=$val . ",";
            }
        }
        $data["purview"] = $p;
		 //print_r($data);
        $return_result = $this->role_model->update(  array("purview"=>$data["purview"]) , $data["id"]);
        //echo $this->db->last_query();die();
        //if ($return_result) {
            redirect("/role");
        //} else {
            //echo "權限編輯失敗";
        //}
		
    }

    public function edit($id = null) {
        $html = $this->init("權限編輯", "jquery", "form");
        $this->load->model('role_model');
        $html["role"] = $this->role_model->get_info($id);
        $this->load->model('department_model');
        $html["department"] = $this->department_model->get_all();
        $this->load->view('role_edit', $html);
    }
	
	public function fix_role() {
		
        
        $this->load->model('role_model');
		
        $d["role_id"] 			= $this->input->post('role_id');        
        $d["field"] 			= $this->input->post('field');
		$d["value"] 			= $this->input->post('value');
		
		$data = array($d["field"] =>$d["value"]);
		
		
		print_r($data);
		echo $d["role_id"];
		 
		$this->role_model->update( $data , $d["role_id"]);
        
    }

    public function edit_submit() {
        
        $this->load->model('role_model');
        $data['id'] = $this->input->post('role_id');
        $data['role_name'] = $this->input->post('role_name');
        $data['department_id'] = $this->input->post('department_id');
        $data['description'] = $this->input->post('description');
        //$data['purview'] = $this->input->post('purview');
        //$data['sequence'] = $this->input->post('sequence');
        $return_result = $this->role_model->update_data($data);
        if ($return_result) {
            redirect("/role");
        } else {
            echo "編輯失敗";
        }
    }

    public function delete($id = null) {
        $this->load->model('role_model');
        
        if ($id != null) {
            $data["id"] = $id;
            //print_r($data);
            $return_result = $this->role_model->del($data);
            if ($return_result) {
                redirect("/role");
            } else {
                echo "刪除失敗";
            }
        } else {
            echo "刪除失敗";
        }
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */